package com.anhry.app.safety.manage.tjfx.struts.base;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.anhry.app.safety.manage.enterprise.bean.TbCorpType;
import com.anhry.app.safety.manage.system.bean.TbUser;
import com.anhry.app.safety.manage.system.bean.TbUserDept;
import com.anhry.app.safety.manage.system.service.SystemService;
import com.anhry.app.safety.manage.tjfx.service.TjfxService;
import com.anhry.app.safety.util.Loggers;
import com.anhry.app.safety.util.Page;
import com.anhry.app.safety.util.Util;
import com.anhry.app.safety.util.action.UtilBaseAction;

@Controller
@SuppressWarnings("unchecked")
@Scope("prototype")
public class TjfxCorpBaseAction extends UtilBaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Resource
	protected TjfxService tjfxService;

	@Resource
	protected SystemService systemService;

	private String corpType; // 企业类别

	private String corpTypeMin; // 企业小类

	private String cCounty; // 属地

	private String cUnit;// 乡镇

	private String cHy;// 行业

	private String cGrade;// 查询级别

	private String beginTime;// 开始时间

	private String endTime;// 结束时间

	private String search;// 解码标识

	private String key;// 关键字

	private String num;

	private String size;

	private String groupByString;

	private String sqlKey;

	private Page page;

	private String gradeId;

	private String unitId;

	private String result;

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public SystemService getSystemService() {
		return systemService;
	}

	public void setSystemService(SystemService systemService) {
		this.systemService = systemService;
	}

	public TjfxService getTjfxService() {
		return tjfxService;
	}

	public void setTjfxService(TjfxService tjfxService) {
		this.tjfxService = tjfxService;
	}

	/**
	 * 初始页码
	 * 
	 * @param num
	 *            页数
	 * @return 页数
	 */
	public int setPageNum(int num) {
		return (this.num != null && !"".equals(this.num)) ? Integer.parseInt(this.num) : num;
	}

	/**
	 * 定义分页的每页显示的初始个数
	 * 
	 * @param size
	 *            显示数
	 * @return 显示数
	 */
	public int setPageSize(int size) {
		return (this.size != null && !"".equals(this.size)) ? Integer.parseInt(this.size) : size;
	}

	/**
	 * @param gradeId
	 *            7属地
	 * @param unitId
	 * @return
	 */
	public List getDeptList() {
		try {
			TbUserDept sysDept = this.systemService.getDeptByCode(Util.DEPT_CODE);
			TbUser user = (TbUser) this.contextPvd.getSessionAttr(Util.SESSION_ADMINUSER);
			int grade = Integer.parseInt(user.getTbUserGrade().getId() + "");
			StringBuilder hql;
			if (user.getTbUserDept() == null) {
				hql = new StringBuilder("from TbUserDept t where 1=1 and t.isDele=0");
			} else {
				hql = new StringBuilder("from TbUserDept t where 1=1 and t.isDele=0");
			}
			if (grade == 7) {// 属地登陆
				if (user.getTbUserDept() != null) {
					hql.append(" and t.id=").append(user.getTbUserDept().getId());
				} else {
					hql.append(" and t.id=").append(user.getTbUserUnit().getId());
				}
			} else if (grade == 6 || grade == 4 || grade == 8) {// 区行业，科室，安办
				hql.append(" and t.id=").append(user.getTbUserUnit().getId());
			} else {
				if (null != unitId) {
					hql.append(" and t.deptParentId=" + unitId + " and t.deptType=3");
				} else {
					hql.append(" and t.deptType=3 and t.deptParentId=" + sysDept.getId());
				}
			}
			List<TbUserDept> list = (List) this.utilService.getHqlQuery(hql.toString());
			return list;
		} catch (Exception e) {
			Loggers.error("msgAction:getDeptList()出错：" + e.getMessage());
			e.printStackTrace();
		}
		return null;
	}

	public List getHyList() {
		try {
			List returnList = new ArrayList();
			TbUser user = (TbUser) this.contextPvd.getSessionAttr(Util.SESSION_ADMINUSER);
			int grade = Integer.parseInt(user.getTbUserGrade().getId() + "");
			StringBuffer hqlString = new StringBuffer("from TbUserDept t where 1=1 and ((t.deptType=1 AND t.deptClass=3) or (t.deptType=2 and t.deptClass=1)) and t.deptGrade=2");

			if (user.getTbUserDept() != null && user.getTbUserDept().getDeptType().equals("2") && user.getTbUserDept().getDeptClass().equals("2")) {

			} else if (user.getTbUserUnit() != null && user.getTbUserUnit().getDeptType().equals("2") && user.getTbUserUnit().getDeptClass().equals("2")) {

			} else {
				if (grade == 5) {// 市行业
					hqlString.append(" and t.id=").append(user.getTbUserUnit().getId());
				} else if (grade == 6) {// 区行业
					hqlString.append(" and t.id=").append(user.getTbUserDept().getDeptSj());
				} else if (user.getTbUserDept() != null && (user.getTbUserDept().getId().toString().equals("3281") || user.getTbUserDept().getId().toString().equals("5291"))) {
					hqlString.append(" and t.id=").append(user.getTbUserDept().getId());
				}
			}
			hqlString.append(" order by t.deptType,t.id");
			List<TbUserDept> list = (List) this.utilService.getHqlQuery(hqlString.toString());
			for (TbUserDept dept : list) {
				if ("1".equals(dept.getDeptType()) && "3".equals(dept.getDeptClass()) && "2".equals(dept.getDeptGrade())) {
					dept.setDeptName("安监局-" + dept.getDeptName());
					returnList.add(dept);
				}
				if ("2".equals(dept.getDeptType()) && "2".equals(dept.getDeptGrade()) || "3".equals(dept.getDeptGrade())) {
					returnList.add(dept);
				}
			}
			return returnList;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public List getMaxTypeList(String deptId) {
		try {
			String hql = "";
			TbUserDept d = new TbUserDept();
			if (deptId != null && !deptId.equals("")) {
				d = (TbUserDept) this.utilService.getObjectById(TbUserDept.class, Long.parseLong(deptId));
			}

			if ("1".equals(d.getDeptType()) && "2".equals(d.getDeptGrade()) && "3".equals(d.getDeptClass())) {// 市科室的行业科室
				hql = "from TbCorpType t where 1=1 and t.id in(select distinct(x.typeParentId) from TbCorpType x where x.unitId=" + d.getId() + " and x.isDele=0)";
			} else if ("1".equals(d.getDeptType()) && "3".equals(d.getDeptGrade()) && "3".equals(d.getDeptClass())) {// 区科室
				hql = "from TbCorpType t where 1=1 and t.id in(select distinct(x.typeParentId) from TbCorpType x where x.typeQudeptIds like '%" + d.getId() + "%' and x.isDele=0)";
			} else if ("2".equals(d.getDeptType()) && "2".equals(d.getDeptGrade()) && "1".equals(d.getDeptClass())) {// 市行业
				hql = "from TbCorpType t where 1=1 and t.id in(select distinct(x.typeParentId) from TbCorpType x where x.unitId=" + d.getId() + " and x.isDele=0)";
			} else if ("2".equals(d.getDeptType()) && "3".equals(d.getDeptGrade()) && "1".equals(d.getDeptClass())) {// 区行业
				hql = "from TbCorpType t where 1=1 and t.id in(select distinct(x.typeParentId) from TbCorpType x where x.unitId=" + d.getDeptSj() + " and x.isDele=0)";
			} else {
				hql = "from TbCorpType t where 1=1 and t.typeParentId=0";
			}
			hql += " and t.isDele=0  order by t.id";
			List list = (List) this.utilService.getHqlQuery(hql);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public List getMinTypeList(String typeId, List<String> maxTypeIds, String unitId) {
		TbUser user = (TbUser) this.contextPvd.getSessionAttr(Util.SESSION_ADMINUSER);
		String grade = user.getTbUserGrade().getId().toString();
		List<TbCorpType> typeList = (List) this.contextPvd.getServletContextAttr(Util.ALL_CORP_TYPE);
		List list = new ArrayList();
		try {
			if (null != typeList && typeList.size() > 0) {
				if (typeId != null && !typeId.equals("")) { // 如果点击的大类。查小类
					for (TbCorpType type : typeList) {
						if ("3".equals(grade) && "3".equals(user.getTbUserDept().getDeptClass())) {// 市科室的行业监管部门
							if (type.getTypeParentId().toString().equals(typeId) && type.getUnitId().toString().equals(user.getTbUserDept().getId().toString())) {
								list.add(type);
							}
						} else if ("5".equals(grade) && "1".equals(user.getTbUserUnit().getDeptClass())) {// 市行业，只取得其监管的企业类型
							if (type.getTypeParentId().toString().equals(typeId) && type.getUnitId().toString().equals(user.getTbUserUnit().getId().toString())) {
								list.add(type);
							}
						} else if ("6".equals(grade) && "1".equals(user.getTbUserDept().getDeptClass())) {// 区行业，取得上级市行业相同企业类型
							if (type.getTypeParentId().toString().equals(typeId) && type.getUnitId().toString().equals(user.getTbUserDept().getDeptSj().toString())) {
								list.add(type);
							}
						} else if ("4".equals(grade) && "3".equals(user.getTbUserDept().getDeptClass())) {// 区科室行业部门
							list = (List) this.utilService.getHqlQuery("from TbCorpType t where 1=1 and t.typeQudeptIds like '%" + user.getTbUserDept().getId() + "%' and t.isDele=0 and t.typeParentId=" + typeId + " order by t.id");
						} else {
							if (type.getTypeParentId().toString().equals(typeId)) {
								list.add(type);
							}
						}
					}
				} else { // 按行业和属地查所有符合权限的小类
					for (String s : maxTypeIds) {
						TbCorpType maxType = (TbCorpType) this.utilService.getObjectById(TbCorpType.class, Long.parseLong(s));
						TbUserDept dept = (TbUserDept) this.utilService.getObjectById(TbUserDept.class, Long.parseLong(unitId));
						for (TbCorpType type : typeList) {
							if (dept.getDeptGrade() == null) {// 点击属地
								if (type.getTypeParentId().toString().equals(maxType.getId().toString())) {
									list.add(type);
								}
							} else if (dept.getDeptGrade().equals("2") && dept.getDeptType().equals("1") && dept.getDeptClass().equals("3")) {// 市科室行业部门
								if (type.getTypeParentId().toString().equals(maxType.getId().toString()) && type.getUnitId().toString().equals(unitId.toString())) {
									list.add(type);
								}
							} else if (dept.getDeptGrade().equals("2") && dept.getDeptType().equals("2") && dept.getDeptClass().equals("1")) {// 市行业，只取得其监管的企业类型
								if (type.getTypeParentId().toString().equals(maxType.getId().toString()) && type.getUnitId().toString().equals(unitId.toString())) {
									list.add(type);
								}
							} else if (dept.getDeptGrade().equals("3") && dept.getDeptType().equals("2") && dept.getDeptClass().equals("1")) {// 区行业，取得上级市行业相同企业类型
								if (type.getTypeParentId().toString().equals(maxType.getId().toString()) && type.getUnitId().toString().equals(dept.getDeptSj().toString())) {
									list.add(type);
								}
							} else if (dept.getDeptGrade().equals("3") && dept.getDeptType().equals("1") && "3".equals(dept.getDeptClass())) {// 区科室行业部门
								list = (List) this.utilService.getHqlQuery("from TbCorpType t where 1=1 and t.typeQudeptIds like '%" + user.getTbUserDept().getId() + "%' and t.isDele=0 and t.typeParentId=" + maxType.getId().toString() + " order by t.id");
							} else {
								if (type.getTypeParentId().toString().equals(maxType.getId().toString())) {
									list.add(type);
								}
							}
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	public List getUnitList(String unitId) {
		try {
			String hqlString;
			TbUserDept dept = (TbUserDept) this.utilService.getObjectById(TbUserDept.class, Long.parseLong(unitId));
			if (dept.getDeptType().equals("3") && dept.getDeptClass() != null) {// 乡镇级别
				hqlString = "FROM TbUserDept d WHERE d.id=" + unitId + " AND d.deptType=3 order by d.id asc";
			} else {// 区
				hqlString = "FROM TbUserDept d WHERE d.deptParentId=" + unitId + " AND d.deptType=3 order by d.id asc";
			}
			return (List) this.utilService.getHqlQuery(hqlString);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;

	}

	public String getSearch() {
		return search;
	}

	public void setSearch(String search) {
		this.search = search;
	}

	public String getBeginTime() {
		return beginTime;
	}

	public void setBeginTime(String beginTime) {
		this.beginTime = beginTime;
	}

	public String getCCounty() {
		return cCounty;
	}

	public void setCCounty(String county) {
		cCounty = county;
	}

	public String getCGrade() {
		return cGrade;
	}

	public void setCGrade(String grade) {
		cGrade = grade;
	}

	public String getCHy() {
		return cHy;
	}

	public void setCHy(String hy) {
		cHy = hy;
	}

	public String getCorpType() {
		return corpType;
	}

	public void setCorpType(String corpType) {
		this.corpType = corpType;
	}

	public String getCorpTypeMin() {
		return corpTypeMin;
	}

	public void setCorpTypeMin(String corpTypeMin) {
		this.corpTypeMin = corpTypeMin;
	}

	public String getCUnit() {
		return cUnit;
	}

	public void setCUnit(String unit) {
		cUnit = unit;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public String getGradeId() {
		return gradeId;
	}

	public void setGradeId(String gradeId) {
		this.gradeId = gradeId;
	}

	public String getGroupByString() {
		return groupByString;
	}

	public void setGroupByString(String groupByString) {
		this.groupByString = groupByString;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getNum() {
		return num;
	}

	public void setNum(String num) {
		this.num = num;
	}

	public String getSize() {
		return size;
	}

	public void setSize(String size) {
		this.size = size;
	}

	public String getSqlKey() {
		return sqlKey;
	}

	public void setSqlKey(String sqlKey) {
		this.sqlKey = sqlKey;
	}

	public String getUnitId() {
		return unitId;
	}

	public void setUnitId(String unitId) {
		this.unitId = unitId;
	}
}
